package com.sky.service.impl;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Lemon
 * @Description: 分类相关接口
 * @Date: 2024/12/27 16:01
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * @Author Lemon
     * @Description 修改分类
     * @Date 2024/12/27 16:04
     * @Param [categoryDTO]
     * @Return void
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.updateCategory(category);
    }

    /**
     * @Author Lemon
     * @Description 分页查询
     * @Date 2025-01-06 15:17
     * @Param [categoryPageQueryDTO]
     * @Return java.lang.Object
     */
    @Override
    public Object pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        Map<String, Object> dataMap = new HashMap<>();

        //配置分页查询条件
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        int page1 = (page - 1) * pageSize;
        categoryPageQueryDTO.setPage(page1);
        //分页查询数据
        List<Category> categoryList= categoryMapper.pageCategory(categoryPageQueryDTO);

        //查询数据条数
        int total = categoryMapper.totalCategory(categoryPageQueryDTO);

        //封装返回数据
        dataMap.put("records",categoryList);
        dataMap.put("total",total);

        return dataMap;
    }

    /**
     * @Author Lemon
     * @Description 启用、禁用分类
     * @Date 2025-01-06 15:44
     * @Param [status, id]
     * @Return void
     */
    @Override
    public void updateStatus(String status, String id) {
        categoryMapper.updateStatus(status,id);
    }

    /**
     * @Author Lemon
     * @Description 新增分类
     * @Date 2025-01-06 16:30
     * @Param [category]
     * @Return void
     */
    @Override
    public void addCategory(Category category) {
        categoryMapper.addCategory(category);
    }

    /**
     * @Author Lemon
     * @Description 根据id删除分类
     * @Date 2025-01-06 16:38
     * @Param [id]
     * @Return void
     */
    @Override
    public void deleteCategory(String id) {
        categoryMapper.deleteCategory(id);
    }
}
